import 'package:flutter/material.dart';
import '../model/home_mo.dart';
import '../model/video_model.dart';
import '../navigator/hi_navigator.dart';
import '../util/format_util.dart';
import '../util/view_util.dart';

///视频卡片
class VideoCard extends StatelessWidget {
    final VideoModel videoMo;

    const VideoCard({Key key, this.videoMo}) : super(key: key);

    @override
    Widget build(BuildContext context) {
        return InkWell(
            onTap: () {
                HiNavigator.getInstance().onJumpTo(RouteStatus.detail, args: {"videoMo": videoMo});
            },
            child: SizedBox(
                height: 200,
                child: Card(
                    //取消卡片默认边距
                    margin: EdgeInsets.only(left: 4, right: 4, bottom: 8),
                    child: ClipRRect(
                        borderRadius: BorderRadius.circular(5),
                        child: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [_itemImage(context), _infoText()],
                        ),
                    ),
                ),
            ));
    }

    _itemImage(BuildContext context) {
        final size = MediaQuery.of(context).size;
        return Stack(
            children: [
                cachedImage(videoMo.cover, width: size.width / 2 - 10, height: 120),
                Positioned(
                    left: 0,
                    right: 0,
                    bottom: 0,
                    child: Container(
                        padding: EdgeInsets.only(left: 8, right: 8, bottom: 3, top: 5),
                        decoration: BoxDecoration(
                            //渐变
                            gradient: LinearGradient(
                                begin: Alignment.bottomCenter,
                                end: Alignment.topCenter,
                                colors: [Colors.black54, Colors.transparent])),
                        child: Row(
                            mainAxisAlignment: MainAxisAlignment.spaceBetween,
                            children: [
                                _iconText(Icons.ondemand_video, videoMo.view),
                                _iconText(Icons.favorite_border, videoMo.favorite),
                                _iconText(null, videoMo.duration),
                            ],
                        ),
                    ))
            ],
        );
    }

    _iconText(IconData iconData, int count) {
        String views = "";
        if (iconData != null) {
            views = countFormat(count);
        } else {
            views = durationTransform(videoMo.duration);
        }
        return Row(
            children: [
                if (iconData != null) Icon(iconData, color: Colors.white, size: 12),
                Padding(
                    padding: EdgeInsets.only(left: 3),
                    child: Text(views, style: TextStyle(color: Colors.white, fontSize: 10)))
            ],
        );
    }

    _infoText() {
        return Expanded(
            child: Container(
                padding: EdgeInsets.only(top: 5, left: 8, right: 8, bottom: 5),
                child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                        Text(
                            videoMo.title,
                            maxLines: 2,
                            overflow: TextOverflow.ellipsis,
                            style: TextStyle(fontSize: 12, color: Colors.black87),
                        ),
                        //作者
                        _owner()
                    ],
                ),
            ));
    }

    _owner() {
        var owner = videoMo.owner;
        return Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
                Row(
                    children: [
                        ClipRRect(
                            borderRadius: BorderRadius.circular(12),
                            child: cachedImage(owner.face, height: 24, width: 24)),
                        Padding(
                            padding: EdgeInsets.only(left: 8),
                            child: Text(
                                owner.name,
                                style: TextStyle(fontSize: 11, color: Colors.black87),
                            ),
                        )
                    ],
                ),
                Icon(
                    Icons.more_vert_sharp,
                    size: 15,
                    color: Colors.grey,
                )
            ],
        );
    }
}